using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Web;
using Sedna.Core.Common.Objects.Navigation;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Globalization;

namespace Sedna.Core.Controls.Actions
{
	/// <summary>
	/// Summary description for LoadStaticListAction.
	/// </summary>
	public class LoadStaticListAction : AbstractAction
	{
		private IList items;
		private IMessageManager messageManager;
		private string dataSourcePath=null;
		private string selectedIndexPath=null;
//		private string dataValueFieldPath;
//		private string dataTextFieldPath;


		public string DataSourcePath
		{
			set { dataSourcePath = value; }
		}

		public string SelectedIndexPath
		{
			set { selectedIndexPath = value; }
		}

        protected string GetSelectedIndexPath()
        {
            return this.selectedIndexPath;
        }

		public IList Items
		{
			get
			{
				return items;
			}
			set
			{
				items = value;
			}
		}

		public IMessageManager MessageManager
		{
			set { messageManager = value; }
		}


		public override IActionResult Execute(IExecutionContext executionContext)
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("Value", typeof(string));
			dt.Columns.Add("Text", typeof(string));

			int counter = 0;
			int selectedIndex = -1;

			foreach(ListItem item in items)
			{
				if(item.Selected && selectedIndex < 0)
				{
					selectedIndex = counter;
				}
				dt.Rows.Add(new object[]{item.Value, messageManager.GetMessage(item.TextKey, item.Text)});
				counter++;
			}

			//--Obsolete--
			if(selectedIndex >= 0 && executionContext.GetFromCurrentScope("selectedIndex") != null)
			{
				executionContext.SetToCurrentScope("selectedIndex", selectedIndex);
			}
			executionContext.SetToCurrentScope("dataValueField", "Value");
			executionContext.SetToCurrentScope("dataTextField", "Text");
			executionContext.SetToCurrentScope("dataSource", dt.DefaultView);
			//----

			if (dataSourcePath!=null)
			{
				executionContext.SetPropertyValue(dataSourcePath, dt.DefaultView);
			}
			// TODO:			
			if (selectedIndexPath != null) 
			{
				IScopePath iScopePath = scopePathParser.Parse(selectedIndexPath);

				if (iScopePath == null || Convert.ToInt32(iScopePath.Value) < 0)
					executionContext.SetPropertyValue(selectedIndexPath, selectedIndex);
			}
						

			return CreateResult(successStatus);
		}


		public class ListItem
		{
			private bool selected = false;
			private string value;
			private string text;
			private string textKey;

			public bool Selected
			{
				get { return selected; }
				set { selected = value; }
			}

			public string Value
			{
				get { return this.value; }
				set { this.value = value; }
			}

			public string Text
			{
				get { return text; }
				set { text = value; }
			}

			public string TextKey
			{
				get { return textKey!=null?textKey:text; }
				set { textKey = value; }
			}
		}
	}
}
